home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / NETUSER.C < prev    next >
C/C++ Source or Header  |  1988-07-28  |  3KB  |  138 lines

  1. /* Miscellaneous format conversion subroutines */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "netuser.h"
  7. int net_error;
  8.  
  9. #define LINELEN 256
  10.  
  11. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  12.  * binary IP address
  13.  */
  14. int32
  15. aton(s)
  16. register char *s;
  17. {
  18.     int32 n;
  19.     int atoi();
  20.     register int i;
  21.  
  22.     n = 0;
  23.     for(i=24;i>=0;i -= 8){
  24.         n |= (int32)atoi(s) << i;
  25.         if((s = index(s,'.')) == NULLCHAR)
  26.         break;
  27.         s++;
  28.     }
  29.     return n;
  30. }
  31. /* Resolve a host name into an IP address. IP addresses in dotted-decimal
  32.  * notation are distinguished from domain names by enclosing them in
  33.  * brackets, e.g., [44.64.0.1]
  34.  */
  35. int32
  36. resolve(host)
  37. char *host;
  38. {
  39.     register char *cp,*cp1;
  40.     int i;
  41.     char hostent[LINELEN];
  42.     FILE *sfile;
  43.  
  44.     if(*host == '['){
  45.         /* Brackets indicate IP address in dotted-decimal form */
  46.         return aton(host + 1);
  47.     }
  48.     /* Not a numerical IP address, search the host table */
  49.     if((sfile = fopen(hosts,"r")) == NULL){
  50.         return 0;
  51.     }
  52.     while (!feof(sfile)){
  53.         fgets(hostent,LINELEN,sfile);
  54.         rip(hostent);
  55.         cp = hostent;
  56.         if(*cp == '#' || !isdigit(*cp))
  57.             continue;    /* Comment or invalid line */
  58.         while(cp != NULLCHAR){
  59.             /* Skip white space */
  60.             while(*cp == ' ' || *cp == '\t')
  61.                 cp++;
  62.             if(*cp == '\0')
  63.                 break;
  64.             /* Look for next token, find length of this one */
  65.             if((cp1 = index(cp,'\t')) != NULLCHAR){
  66.                 i = cp1 - cp;
  67.             } else if((cp1 = index(cp,' ')) != NULLCHAR) {
  68.                 i = cp1 - cp;
  69.             } else {
  70.                 i = strlen(cp);
  71.             }
  72.             if(strlen(host) == i && strncmp(host,cp,i) == 0){
  73.                 /* Found it */
  74.                 fclose(sfile);
  75.                 return aton(hostent);
  76.             }
  77.             /* That one didn't match, try the next one */
  78.             cp = cp1;
  79.         }
  80.     }
  81.     /* No address found */
  82.     fclose(sfile);
  83.     return 0;
  84. }
  85.  
  86. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  87.  * string, e.g., 255.255.255.255\0
  88.  */
  89. char *
  90. inet_ntoa(a)
  91. int32 a;
  92. {
  93.     static char buf[16];
  94.  
  95.     sprintf(buf,"%u.%u.%u.%u",
  96.         hibyte(hiword(a)),
  97.         lobyte(hiword(a)),
  98.         hibyte(loword(a)),
  99.         lobyte(loword(a)) );
  100.     return buf;
  101. }
  102. /* Convert a socket (address + port) to an ascii string of the form
  103.  * aaa.aaa.aaa.aaa:ppppp
  104.  */
  105. char *
  106. psocket(s)
  107. struct socket *s;
  108. {
  109.     static char buf[30];
  110.  
  111.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  112.     return buf;
  113. }
  114. /* Convert hex-ascii string to long integer */
  115. long
  116. htol(s)
  117. char *s;
  118. {
  119.     long ret;
  120.     char c;
  121.  
  122.     ret = 0;
  123.     while((c = *s++) != '\0'){
  124. #if ( !ATARI_ST && !LATTICE )    /* DG2KK "ik versta er heelemal niets van!"*/
  125.         c &= 0x7f;
  126. #endif
  127.         if(c >= '0' && c <= '9')
  128.             ret = ret*16 + (c - '0');
  129.         else if(c >= 'a' && c <= 'f')
  130.             ret = ret*16 + (10 + c - 'a');
  131.         else if(c >= 'A' && c <= 'F')
  132.             ret = ret*16 + (10 + c - 'A');
  133.         else
  134.             break;
  135.     }
  136.     return ret;
  137. }
  138.